<?php

namespace Biz\model\trade;

use Biz\validate\trade\ShopAddress as vShopAddress;
use Medoo\Medoo;

class ShopAddress extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_trade';

    const _DATABASE_ = 'shop_trade';

    const _TABLE_NAME = 'shop_address';

    const STATUS_NORMAL = 1;
	const STATUS_DEL = 9;

    protected static $fields = [
        'id',
        'shop_id',
        'accept_name' => ['trim', 'htmlspecialchars'],
        'province',
        'city',
        'area',
        'address' => ['trim', 'htmlspecialchars'],
        'info_address' => ['trim', 'htmlspecialchars'],
        'zipcode',
        'mobile',
        'is_default',
        'remark' => ['trim', 'htmlspecialchars'],
        'addtime',
		'status'
    ];

    public static function getDefaultAddress($shopId)
    {
        return self::get("*", ['shop_id' => $shopId, 'is_default' => 1,'status' => self::STATUS_NORMAL]);
    }

    public static function getById($id)
    {
        return self::get("*", ['id' => $id, 'status' => self::STATUS_NORMAL]);
    }


    public static function hasMasterAddressByShopId($shopId)
    {
        $has = self::has_master(Medoo::raw(' where shop_id=:shop_id and status=:status for update', [':shop_id' => $shopId, ':status' => self::STATUS_NORMAL]));
        return $has;
    }

    public static function insertAddress($data)
    {
        try {
        	$data['status'] = self::STATUS_NORMAL;
            self::_checkFields($data);
            $data['addtime'] = time();
            $data = self::buidData($data);
            self::insert($data);
            return self::id();
        } catch (\Exception $e) {
            throw $e;
        }
    }

    public static function checkUserAddressById($id, $shopId)
    {
        return self::has(['id' => $id, 'shop_id' => $shopId, 'status' => self::STATUS_NORMAL]);
    }

    public static function selectAddressFieldsByShopId($shopId, $fields = '*')
    {
        return self::select($fields, ['shop_id' => $shopId, 'status' => self::STATUS_NORMAL,'ORDER' => ['is_default' => 'DESC', 'addtime' => 'DESC']]);
    }

    public static function deleteAddressById($addressId)
    {
        return self::update(['status' => self::STATUS_DEL],['id' => $addressId]);
    }

    public static function saveAddressById($addressId, $data)
    {
    	$data['status'] = self::STATUS_NORMAL;
        self::_checkFields($data, $addressId);
        $data = self::buidData($data);
        try {
            $result = self::update($data, ['id' => $addressId]);
            return $result->rowCount();
        } catch (\Exception $e) {
            throw $e;
        }
    }

    public static function updateDefaultAddressByShopId($shopId, $default)
    {
        return self::update(['is_default' => $default], ['shop_id' => $shopId, 'status' => self::STATUS_NORMAL]);
    }

    public static function setDefaultAddressById($addressId)
    {
        return self::update(['is_default' => 1], ['id' => $addressId, 'status' => self::STATUS_NORMAL]);
    }

    private static function _checkFields($data, $addressId = null)
    {
        $scenes = is_null($addressId) ? 'insert' : 'update';
        if (!vShopAddress::validate($data, $scenes)) {
            $msg = vShopAddress::firstError();
            throw new \Exception($msg, 0);
        }
    }

    public static function getCount($shopId)
    {
        return self::count(['id'], ['shop_id' => $shopId, 'status' => self::STATUS_NORMAL]);
    }
}